package com.technology.education.ui.common.score_record

import androidx.core.os.bundleOf
import com.angcyo.dsladapter.toDslItemList
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.AppData
import com.technology.education.data.model.Record
import com.technology.education.ext.navigation
import com.technology.education.ext.setPageItems
import me.hgj.jetpackmvvm.ext.parseState

/**
 *@author:create by BZY
 *Date: 2022/2/28 17
 * 积分记录-时长记录
 * type==0 积分记录
 * type==1 时长记录
 */
class ScoreRecordFragment : BaseRecyclerTopBarFragment<ScoreRecordViewModel>() {
    override fun initViews() {
        mViewModel.type = arguments?.getInt("type") ?: 0
        setTitle(if (mViewModel.type == 0) "积分记录" else "时长记录")
    }

    override fun lazyLoadData() {
        loading()
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { it ->
                hasMore(it.total)
                renderAdapter {
                    setPageItems(
                        it.list.toDslItemList(R.layout.item_score_record) {
                            itemBindOverride = { itemHolder, _, _, _ ->
                                itemData?.let { it ->
                                    if (it is Record) {
                                        itemHolder.tv(R.id.tv_title)?.text = it.title
                                        itemHolder.tv(R.id.tv_score)?.text = it.valueString
                                        itemHolder.tv(R.id.tv_date)?.text = it.createDate
                                        itemHolder.clickItem { view ->
                                            //只有提现记录和兑换商品可以点击跳转,判断类型
                                            if (it.haveDetail) {
                                                if (it.billPointType == "3" || it.billDurationType == "6") {
                                                    //兑换现金
                                                    navigation(
                                                        view,
                                                        R.id.action_scoreRecordFragment_to_withDrawDetailFragment,
                                                        bundleOf(
                                                            "id" to if (it.billDurationType == "6") it.id else it.relateIds,
                                                            "type" to if (AppData.MEMBER_TYPE == "0") 2 else if (it.billDurationType == "6") 4 else 1
                                                        )
                                                    )
                                                }
                                                if (it.billPointType == "4") {
                                                    //兑换现金
                                                    navigation(
                                                        view,
                                                        R.id.action_scoreRecordFragment_to_goodsOrderDetailFragment,
                                                        bundleOf(
                                                            "id" to it.relateIds
                                                        )
                                                    )
                                                }

                                            }

                                        }
                                    }
                                }

                            }
                        },
                        mViewModel.pageIndex
                    )
                }
            }, onError = {
                loadError()
            })
        }
    }
}